package J5_14;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

public class test {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        int res = n;
        for (int i = 1; i < m; i++) {
            res = (n - 1) * res % 1000000000;
        }
        System.out.println(res);
    }
    static int[] x = {1,0,-1,0};
    static int[] y = {0,1,0,-1};
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        int x1 = in.nextInt(), y1 = in.nextInt(), x2 = in.nextInt(), y2 = in.nextInt();
        char[][] ch = new char[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            String str = in.next();
            for (int j = 1; j <= m; j++) {
                ch[i][j] = str.charAt(j-1);
            }
        }
        if (ch[x2][y2] == '*') {
            System.out.println(-1);
            return;
        }
        boolean[][] visited = new boolean[n + 2][m + 2];
        for (int i = 0; i < n + 2; i++) {
            visited[i][0] = true;
            visited[i][m + 1] = true;
        }
        for (int i = 1; i < m + 1; i++) {
            visited[0][i] = true;
            visited[n + 1][i] = true;
        }
        Queue<int[]> queue = new ArrayDeque<>();
        queue.add(new int[]{x1, y1});
        int step = 0;
        while (!queue.isEmpty()) {
            int len = queue.size();
            for (int i = 0; i < len; i++) {
                int[] tmp = queue.poll();
                int xcur = tmp[0], ycur = tmp[1];
                if (xcur == x2 && ycur == y2) {
                    System.out.println(step);
                    return;
                }
                visited[xcur][ycur] = true;
                for (int j = 0; j < 4; j++) {
                    int xt = xcur + x[j];
                    int yt = ycur + y[j];
                    if (!visited[xt][yt] && ch[xt][yt] == '.') {
                        queue.add(new int[]{xt, yt});
                    }
                }
            }
            step++;
        }
        System.out.println(-1);
    }


}
